package com.sgai.zero_robot.pop;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgai.zero_robot.R;
import com.sgai.zero_robot.core.RobotManagerFactory;
import com.sgai.zero_robot.pojo.SettingBean;
import com.sgai.zero_robot.utils.DeviceUtils;

import java.util.ArrayList;
import java.util.List;

public class SettingPopupWindow extends BasePopupWindow implements PopupWindow.OnDismissListener {
    private final AppCompatActivity activity;
    private TextView tvTitle;
    private ViewGroup contentRoot;
    private RecyclerView rvSetting;

    public SettingPopupWindow(AppCompatActivity context) {
        super(context, DeviceUtils.dp2px(context, 360), 0);
        this.activity = context;
    }

    @Override
    public void init() {
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setAnimationStyle(R.style.RightAnimStyle);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x66000000));
        mPopupWindow.setOnDismissListener(this);
        tvTitle = findviewById(R.id.tv_title);
        contentRoot = findviewById(R.id.ll_content_root);
        rvSetting = findviewById(R.id.rv_setting);
        mList = new ArrayList<>();
        initData();
    }
    private  List<SettingBean> mList;
    private void initData(){
        addInfo("履带配置",R.mipmap.moving_speed_ratio_pre,R.mipmap.moving_speed_ratio_nor);
        addInfo("探针配置",R.mipmap.swing_angle_pre,R.mipmap.swing_angle_nor);
        addInfo("视频配置",R.mipmap.video_record_pre,R.mipmap.video_record_nor);
        addInfo("摄像头配置",R.mipmap.camera_pre,R.mipmap.camera_nor);
        addInfo("电池信息",R.mipmap.battery_pre,R.mipmap.battery_nor);
        mAdapter = new SettingAdapter(mContext, mList, new SettingCallback() {
            @Override
            public void onSelect(int position, String title) {
                contentRoot.removeAllViews();
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                if (position == 0){
                    contentRoot.addView(new SettingSpeedRatioView(activity),layoutParams);
                }else if (position == 1){
                    contentRoot.addView(new SettingSwingAngleView(activity),layoutParams);
                }else if (position == 2){
                    contentRoot.addView(new SettingVideoView(activity),layoutParams);
                }else if (position == 3){
                    contentRoot.addView(new SettingCameraView(activity),layoutParams);
                }else if (position == 4){
                    contentRoot.addView(new SettingBatteryView(activity),layoutParams);
                }
                tvTitle.setText(title);
            }
        });
        rvSetting.setLayoutManager(new LinearLayoutManager(mContext));
//        rvSetting.addItemDecoration(new SpaceItemDecoration(70));
        rvSetting.setAdapter(mAdapter);
        mAdapter.setSelectIndex(0);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        contentRoot.addView(new SettingSpeedRatioView(mContext),layoutParams);
        tvTitle.setText(mList.get(mAdapter.getSelectIndex()).title);
    }
    private SettingAdapter mAdapter;
    private void addInfo(String title,int resPre,int resNor){
        SettingBean settingBean = new SettingBean();
        settingBean.title = title;
        settingBean.resNor = resNor;
        settingBean.resPre = resPre;
        mList.add(settingBean);
    }
    @Override
    public int getContentView() {
        return R.layout.pop_setting;
    }

    @Override
    public void showAtLocation(Activity activity, View view, int gravity, int x, int y) {
        super.showAtLocation(activity, view, gravity, x, y);
        RobotManagerFactory.getInstance().startConfig();
    }

    @Override
    public void onDismiss() {
        RobotManagerFactory.getInstance().endConfig();
    }
}
